<template>
  <div>
    <h1>【aty-tag】 component demo</h1>
    <p>【aty-tag】 本质</p>
    <aty-row class="fd-tag-box">
      <aty-row>
        <aty-title level="4">1.【aty-tag】 基础</aty-title>
        <aty-tag :closable="false" text="标签一" checkable @change="change" />
        <aty-tag checkable :checked="false" :closable="false" color="blue">标签二</aty-tag>
        <aty-tag v-if="show" closable @close="handleClose" name="标签三">标签三</aty-tag>
      </aty-row>
      <aty-row>
        <aty-title level="4">2.【aty-tag】 type</aty-title>
        <aty-tag type="border">标签一</aty-tag>
        <aty-tag type="border">标签二</aty-tag>
        <aty-tag type="dot">标签三</aty-tag>
        <aty-tag type="dot">标签四</aty-tag>
      </aty-row>
      <aty-row>
        <aty-title level="4">3.【aty-tag】 color</aty-title>
        <aty-row>
          <aty-tag color="blue">标签一</aty-tag>
          <aty-tag color="green">标签二</aty-tag>
          <aty-tag color="red">标签三</aty-tag>
          <aty-tag color="yellow">标签四</aty-tag>
        </aty-row>
        <aty-tag type="border" color="blue">标签一</aty-tag>
        <aty-tag type="border" color="green">标签二</aty-tag>
        <aty-tag type="border" color="red">标签三</aty-tag>
        <aty-tag type="border" color="yellow">标签四</aty-tag>
        <aty-row>
          <aty-tag type="dot" color="blue">标签一</aty-tag>
          <aty-tag type="dot" color="green">标签二</aty-tag>
          <aty-tag type="dot" color="red" @click="click" text="标签三" />
          <aty-tag type="dot" checkable @change="change" color="yellow">标签四</aty-tag>
        </aty-row>
      </aty-row>
      <aty-row>
        <aty-title level="4">5.【aty-tag】 动态添加</aty-title>
        <aty-row>
          <aty-tag
            :key="tag"
            v-for="tag in dynamicTags"
            closable
            :disable-transitions="false"
            @close="handleClose(tag)"
          >{{tag}}</aty-tag>
          <aty-input
            class="input-new-tag"
            v-if="inputVisible"
            v-model="inputValue"
            ref="saveTagInput"
            size="small"
            @keyup.enter.native="handleInputConfirm"
            @blur="handleInputConfirm"
          ></aty-input>
          <aty-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag</aty-button>
        </aty-row>
      </aty-row>
      <aty-row>
        <aty-title level="4">6.【aty-tag】 迷之颜色</aty-title>
        <aty-row>
          <aty-tag v-for="(color, index) in arr" :key="index" :closable="false" :color="color">标签{{color}}</aty-tag>
        </aty-row>
      </aty-row>
    </aty-row>
  </div>
</template>
<script>
export default {
  data() {
    return {
      show: true,
      dynamicTags: ["标签一", "标签二", "标签三"],
      inputVisible: false,
      inputValue: "",
      arr: []
    };
  },
  methods: {
    color16() {
      var r = Math.floor(Math.random() * 256);
      var g = Math.floor(Math.random() * 256);
      var b = Math.floor(Math.random() * 256);
      var color = "#" + r.toString(16) + g.toString(16) + b.toString(16);
      return color;
    },
    handleClose: function(event, name) {
      this.show = false;
      alert("name:" + name);
    },
    change(checked, name) {
      console.log(checked, name);
    },
    click(text, name) {
      console.log(text, name);
    },
    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },
    showInput() {
      this.inputVisible = true;
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },
    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    }
  },
  mounted() {
    for (let i = 0; i < 30; i++) {
      const color = this.color16();
      this.arr.push(color);
    }
  }
};
</script>
<style  lang="less" type="text/less"  >
.fd-tag-box {
  margin: 20px;
  text-align: left;
}
.aty-tag + .aty-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
